<?php
/**
 * 代金券类
 */
namespace Home\Controller;
use Think\Controller;
class VoucherController extends CommonController {

    /**
     * 代金券列表
     */
    public function lists(){
        $template = C('DB_PREFIX').'voucher_template';
        $voucher = C('DB_PREFIX').'voucher';
        $uid = I('post.uid',0,'intval');
        if(!$uid) returnApiError(1,'uid参数错误');
        $state = I('post.state');

        if($state == 1){
            $where["$voucher.voucher_state"] = 1;
        }
        if($state == 2){
            $where["$voucher.voucher_state"] = 2;
        }
        if($state == 3){
            $where["$voucher.voucher_state"] = 3;
        }
        if($state == 4){
            $where["$voucher.voucher_state"] = 4;
        }

        $model = M('voucher');
        $pindex = I('post.pno',1,'intval') ;     //当前页码
        $psize  = I('post.pgz',10,'intval') ;   //显示条数
        if(!$pindex)  $pindex = 1;
        $pfirst = ($pindex - 1) * $psize;


        $field = "$voucher.voucher_id,$voucher.voucher_title,$voucher.voucher_desc,$voucher.voucher_start_date,$voucher.voucher_end_date,$voucher.voucher_price,$voucher.voucher_state,$template.voucher_t_customimg";
        $data = $model->field($field)->where($where)->join("$template ON $voucher.voucher_t_id = $template.voucher_t_id and $voucher.voucher_owner_id = $uid")->limit($pfirst .','.$psize)->select();
        foreach($data as $k=>$v){
            if($v['voucher_end_date'] > time()){
                $data[$k]['voucher_state'] = 3;
            }
            $data[$k]['voucher_t_customimg'] = SHOP_SITE_URL.'/data/upload/shop/common/'.$data[$k]['voucher_t_customimg'];
        }
        if($data){
            returnApiSuccess('ok',$data);
        }
        returnArr('数据为空');
    }

    /**
     * 领取代金券
     */
    public function exchange(){
        $uid = I('post.uid',0,'intval');
        $code = I('post.code',0,'intval');
        if(!$code) returnApiError(1,'code参数错误');
        if(!$uid) returnApiError(1,'uid参数错误');

        $voucher_template = M('voucher_template');
        $voucher = M('voucher');
        $member = M('member');

        //获取用户积分
        $member_data = $member->find($uid);
        if(!$member) returnApiError(1,'会员不存在');
        $member_points = $member_data['member_points'];

        //代金券模板信息
        $template_data = $voucher_template->where("voucher_t_id = $code")->find();
        if(!$template_data) {
            returnApiError(1,'代金券不存在');
        }

        //代金券状态
        if($template_data['voucher_t_state'] != 1)  returnApiError(1,'代金券已失效');

        //代金券过期时间

        if($template_data['voucher_t_end_date'] < time())  returnApiError(1,'代金券已过期');
        if($template_data['voucher_t_start_date'] > time())  returnApiError(1,'代金券未发放');

        //代金券数量
        if($template_data['voucher_t_total'] == $template_data['voucher_t_giveout'] ){
            returnApiError(1,'代金券已经领完');
        }

        //限领
        $count = $voucher->where("voucher_owner_id = $uid and voucher_t_id = $code")->count();
        if($count >= $template_data['voucher_t_eachlimit']){
            returnApiError(1,'每人限领'.$template_data['voucher_t_eachlimit'].'个');
        }

        //领取方式
        if($template_data['voucher_t_gettype'] == 1){
            //积分判断
            if($member_points < $template_data['voucher_t_points']){
                returnApiError(1,'积分不足');
            }
        }

        $add['voucher_code'] = $this->get_voucher_code($uid);
        $add['voucher_t_id'] = $template_data['voucher_t_id'];
        $add['voucher_title'] = $template_data['voucher_t_title'];
        $add['voucher_desc'] = $template_data['voucher_t_desc'];
        $add['voucher_start_date'] = $template_data['voucher_t_start_date'];
        $add['voucher_end_date'] = $template_data['voucher_t_end_date'];
        $add['voucher_price'] = $template_data['voucher_t_price'];
        $add['voucher_limit'] = $template_data['voucher_t_limit'];
        $add['voucher_store_id'] = $template_data['voucher_t_store_id'];
        $add['voucher_state'] = $template_data['voucher_t_state'];
        $add['voucher_active_date'] = $template_data['voucher_t_add_date'];
        $add['voucher_owner_id'] = $uid;
        $add['voucher_owner_name'] = $member_data['member_name'];

        $points = $member_data['member_points'] - $template_data['voucher_t_points'];
        $save['member_points'] = $points;
        if($voucher->add($add)){
            $member->where("member_id = $uid")->save($save);
            returnApiSuccess('ok');
        }
        returnApiError(1,'添加失敗');
    }

    /**
     * 积分商城优惠券
     * @return json
     */
    public function pointshop(){
        $pindex = I('post.pno',1,'intval') ;     //当前页码
        $psize  = I('post.pgz',10,'intval') ;   //显示条数
        if(!$pindex)  $pindex = 1;
        $pfirst = ($pindex - 1) * $psize;

        $voucher = M('voucher_template');
        $data = $voucher->where("voucher_t_state = 1")->limit($pfirst .','.$psize)->select();
        foreach($data as $k=>$v){
            if($data[$k]['voucher_t_end_date'] < time()){
               unset($data[$k]);continue;
            }
        }
        if($data)  returnApiSuccess('ok',$data);
        returnArr('数据为空');
    }

    /*
    * 获取代金券编码
    */
    public function get_voucher_code($member_id = 0){
        static $num = 1;
        $sign_arr = array();
        $sign_arr[] = sprintf('%02d',mt_rand(10,99));
        $sign_arr[] = sprintf('%03d', (float) microtime() * 1000);
        $sign_arr[] = sprintf('%010d',time() - 946656000);
        if($member_id){
            $sign_arr[] = sprintf('%03d', (int) $member_id % 1000);
        } else {
            //自增变量
            $tmpnum = 0;
            if ($num > 99){
                $tmpnum = substr($num, -1, 2);
            } else {
                $tmpnum = $num;
            }
            $sign_arr[] = sprintf('%02d',$tmpnum);
            $sign_arr[] = mt_rand(1,9);
        }
        $code = implode('',$sign_arr);
        $num += 1;
        return $code;
    }

    /**
     * 积分兑换纪录
     */
    public function record(){
        $uid = I('post.uid',0,'intval');
        $pindex = I('post.pno',1,'intval') ;     //当前页码
        $psize  = I('post.pgz',10,'intval') ;   //显示条数
        if(!$pindex)  $pindex = 1;
        $pfirst = ($pindex - 1) * $psize;

        $voucher = M('voucher');
        $v = C('DB_PREFIX').'voucher';
        $t = C('DB_PREFIX').'voucher_template';
        $field = "$v.voucher_id,$v.voucher_title,$t.voucher_t_store_id,$v.voucher_active_date,$v.voucher_code,$t.voucher_t_points,$t.voucher_t_customimg";
        $data = $voucher->field($field)->join("$t ON $v.voucher_t_id = $t.voucher_t_id and $v.voucher_owner_id = $uid")->limit($pfirst .','.$psize)->select();
        foreach($data as $k=>$v){
            $data[$k]['voucher_t_customimg'] = SHOP_SITE_URL.'/data/upload/shop/voucher/'.$data[$k]['voucher_t_store_id'].'/'.$data[$k]['voucher_t_customimg'];
        }
        if($data)  returnApiSuccess('ok',$data);
        returnArr('数据为空');
    }




}